package edu.gyc.hishiro.shiro.utils;

import edu.gyc.hishiro.model.UserInfo;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

public class PasswordEncrypter<T extends UserInfo> {

    private RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
    // 散列算法
    public static String algorithmName = "md5";
    // 散列次数
    public static int  hashIterations = 2;

    public void encryptPassword(T t) {
        // 随机生成盐
        t.setSalt(randomNumberGenerator.nextBytes().toHex());
        // 加密
        String encryptPwd = new SimpleHash(algorithmName, t.getPassword(),
                ByteSource.Util.bytes(t.getSalt()), hashIterations).toHex();
        t.setPassword(encryptPwd);
    }

    public void setAlgorithmName(String algorithmName) {
        this.algorithmName = algorithmName;
    }

    public void setHashIterations(int hashIterations) {
        this.hashIterations = hashIterations;
    }

    public String getAlgorithmName() {
        return algorithmName;
    }

    public int getHashIterations() {
        return hashIterations;
    }
}
